﻿//------------------------------------------------------------
// VRLauncher
// Copyright © 2024-2025.1 Song Ding
// 蓝牙列表的业务控制
//------------------------------------------------------------
using UnityEngine;
using UnityEngine.UI;
using UnityGameFramework.Runtime;
using VRLauncher;

namespace VRLauncher
{
    public class EntityFromListBluetooth : EntityFromListObject
    {
        [SerializeField]
        private EntityFromListBluetoothData m_EntityFromListBluetoothData = null;
        [SerializeField]
        private Button m_contentBluetoothButton;
        [SerializeField]
        private Text m_BluetoothNameText;
        [SerializeField]
        private Text m_bondstateText;
        [SerializeField]
        private FormListBlueComponent m_FormListBlueComponent;
        [SerializeField]
        private UIMainForm m_UIMainForm;
#if UNITY_2017_3_OR_NEWER
        protected override void OnInit(object userData)
#else
        protected internal override void OnInit(object userData)
#endif
        {
            base.OnInit(userData);
            TryGetComponent<FormListBlueComponent>(out m_FormListBlueComponent);
            if (m_FormListBlueComponent == null)
            {
                Log.Error("FormListBlueComponent组件无效.");
                return;
            }
         
            if (!m_FormListBlueComponent.ContentBluetButton||!m_FormListBlueComponent.BluetNameText|| !m_FormListBlueComponent._UIMainForm)
            {
                Log.Error("组件未绑定.");
                return;
            }
            m_UIMainForm = m_FormListBlueComponent._UIMainForm;
            m_BluetoothNameText = m_FormListBlueComponent.BluetNameText;
            m_contentBluetoothButton = m_FormListBlueComponent.ContentBluetButton;
            m_bondstateText=m_FormListBlueComponent.BondstateText;
            m_contentBluetoothButton.onClick.AddListener(OnClickFormListBlue);
        }

#if UNITY_2017_3_OR_NEWER
        protected override void OnShow(object userData)
#else
        protected internal override void OnShow(object userData)
#endif
        {
            base.OnShow(userData);

            m_EntityFromListBluetoothData = userData as EntityFromListBluetoothData;
            if (m_EntityFromListBluetoothData == null)
            {
                Log.Error("UIFromListBluetooth data is invalid.");
                return;
            }
         
            m_BluetoothNameText.text=m_EntityFromListBluetoothData.BluetoothName;
            switch (m_EntityFromListBluetoothData.Bondstate) 
            {
                case 11: m_bondstateText.text = "连接中"; m_bondstateText.color=Color.yellow; break;
                case 12: m_bondstateText.text = "已连接"; m_bondstateText.color = Color.green; break;
                default: m_bondstateText.text = ""; m_bondstateText.color = Color.white; break;

            }
        }
        //点击连接蓝牙
        void OnClickFormListBlue()
        {
            //if (m_UIMainForm.ConnectBluetooth(m_EntityFromListBluetoothData.Address))
            //{
            //    Debug.Log("点击了" + m_EntityFromListBluetoothData.BluetoothName);
            //}
            //else
            //{
            //    Debug.Log("连接" + m_EntityFromListBluetoothData.BluetoothName + "出错");
            //}
           
        }
    }
}

